/*************************************************************************
 *  Copyright (C), 2016-2017, Mogoson tech. Co., Ltd.
 *  FileName: TUEditor.cs
 *  Author: Mogoson   Version: 1.0   Date: 9/27/2016
 *  Version Description:
 *    Internal develop version,mainly to achieve its function.
 *  File Description:
 *    Ignore.
 *  Class List:
 *    <ID>           <name>             <description>
 *     1.           TUEditor               Ignore.
 *  Function List:
 *    <class ID>     <name>             <description>
 *     1.
 *  History:
 *    <ID>    <author>      <time>      <version>      <description>
 *     1.      Mogoson     9/27/2016       1.0        Build this file.
 *************************************************************************/

namespace Developer.TUTool
{
    using UnityEditor;
    using UnityEngine;

    /// <summary>
    /// Terrain Up Editor.
    /// </summary>
    public class TUEditor : ScriptableWizard
    {
        #region Property and Field
        [Tooltip("Target up terrain.")]
        public Terrain terrain;

        [Tooltip("Up height value.")]
        public float upHeight = 0;
        #endregion

        #region Private Method
        [MenuItem("Tool/TUEditor &T")]
        static void ShowTUEditor()
        {
            DisplayWizard("TUEditor", typeof(TUEditor), "Apply");
        }//Show...()_end

        //Window update.
        void OnWizardUpdate()
        {
            if (terrain)
                isValid = true;
            else
                isValid = false;
        }//OnW...()_end

        //Button click.
        void OnWizardCreate()
        {
            var apply = EditorUtility.DisplayDialog(
                "Apply Confirm",
                "Apply operate can not be recovered!\n"
                + "Make sure you have a backup of target terrain data.\n"
                + "Don't set a negative value to the \"Up Height\" unless you know terrain data inside out.",
                "Apply",
                "Cancle"
                );
            if (apply)
            {
                var hmWidth = terrain.terrainData.heightmapWidth;
                var hmHeight = terrain.terrainData.heightmapHeight;
                var heights = terrain.terrainData.GetHeights(0, 0, hmWidth, hmHeight);
                upHeight /= terrain.terrainData.size.y;
                for (int w = 0; w < hmWidth; w++)
                {
                    for (int h = 0; h < hmHeight; h++)
                    {
                        heights[w, h] += upHeight;
                    }
                }
                terrain.terrainData.SetHeights(0, 0, heights);
            }//if()_end
        }//OnW...()_end
        #endregion
    }//class_end
}//namespace_end